package com.qunar.train.mock.util;

import mockit.Deencapsulation;

/**
 * @author: xudong.tai
 * @since: 2018/7/2 Description:
 **/
public class ClassFieldTrick {

    public static void trickStaticFieldValue(Class clazz, String fieldName, Object fieldValue) {
        ClassAccessControl.resetFinalAccess(clazz, fieldName);
        Deencapsulation.setField(clazz, fieldValue);
    }

    //比较通用，兼容trickStaticFieldValue方法
    public static void trickFieldValue(Class clazz, String fieldName, Object fieldValue) {
        try {
            ClassAccessControl.resetFinalAccess(clazz, fieldName);
            Deencapsulation.setField(clazz.newInstance(), fieldName, fieldValue);
        } catch (Exception e) {
            AegisLog.error(e);
        }
    }
}
